package wat.serwerapp.admins;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import wat.ziszcommon.invoker.Method;
import wat.serwerapp.main.Parameters;
import wat.serwerapp.usermgmt.ldap.RoleManager;
import wat.ziszcommon.transport.Module;

/**
 *
 * @author Kamil
 */
public class AddModule implements Method {

   private static Set<String> owners;

   public Set<String> getOwners() {
      if (owners == null) {
         owners = new HashSet<String>();
         owners.add("pl.edu.wat.erp.client.administration.AddPluginClient");
      }
      return owners;
   }

   public String invoke(String parameters) {
      throw new UnsupportedOperationException("Not supported yet.");
   }

   public List invoke(List parameters) {
      Module module = (Module) parameters.get(0);
      try {
         List<byte[]> clazz = module.getClazz();
         List<String> names = module.getClazzNames();

         String path = Parameters.getInstance().getRepositoryDir() + module.getPath().replace('.', '/');
         int endI = path.lastIndexOf("/");
         path = path.substring(0, endI) + "/";

         System.out.println("PATHHHH:" + path);

         for (int i = 0; i < clazz.size(); i++) {
            String classpath = path + names.get(i);
            System.out.println(classpath);
            File file = createClazzFile(classpath);
            byte[] clazzBytes = clazz.get(i);
            FileOutputStream fos;

            fos = new FileOutputStream(file);

            fos.write(clazzBytes);
         }

         byte[] res = module.getResource();
         if (res != null) {
            String classpath = path + "resources/" + names.get(0).substring(0, names.get(0).length() - 6) + ".properties";
            System.out.println("Res File:" + classpath);
            File file = createClazzFile(classpath);
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(res);
         }

         RoleManager.addRight(RoleManager.roleToLdapFormat("admin"), module.getName(), module.getName());

      } catch (NamingException ex) {
         Logger.getLogger(AddModule.class.getName()).log(Level.SEVERE, null, ex);
      } catch (Exception ex) {
         Logger.getLogger(AddModule.class.getName()).log(Level.SEVERE, null, ex);
      }

      return new ArrayList(1);
   }

   private File createClazzFile(String path) {
      String[] dirs = path.split("/");
      String newPath = dirs[0];


      for (int i = 1; i < dirs.length - 1; i++) {
         newPath = newPath + "/" + dirs[i];
         File file = new File(newPath);
         if (!file.exists()) {
            file.mkdir();
         }
      }
      newPath = newPath + "/" + dirs[dirs.length - 1];
      File file = new File(newPath);
      try {
         file.createNewFile();
      } catch (IOException ex) {
         Logger.getLogger(AddModule.class.getName()).log(Level.SEVERE, null, ex);
      }
      return file;
   }
}
